package com.russell.pms.webapp.listener;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * <p>
 * StartupListener class used to initialize and database settings and populate
 * any application-wide drop-downs.
 * <p>
 * Keep in mind that this listener is executed outside of
 * OpenSessionInViewFilter, so if you're using Hibernate you'll have to
 * explicitly initialize all loaded data at the DM or service level to avoid
 * LazyInitializationException. Hibernate.initialize() works well for doing
 * this.
 * 
 * @author <a href="mailto:fvinluan@gmail.com">Francis Vinluan</a>
 */
public class StartupListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent arg0) {

	}

	public void contextInitialized(ServletContextEvent arg0) {
		Logger logger = LoggerFactory.getLogger(StartupListener.class);

		Resource resource = new ClassPathResource("/release.properties");
		String appName = "";
		try {
			Properties props = PropertiesLoaderUtils.loadProperties(resource);
			String name = props.getProperty("application.name");
			String version = props.getProperty("application.version");
			String build = props.getProperty("application.build.num");

			appName = name + "-" + version + "-" + build;
		} catch (IOException e) {

		}
		String display = StringUtils.rightPad(appName, 22);

		StringBuilder sb = new StringBuilder();
		sb.append("\n");
		sb.append("\n");
		sb.append("     .--.                                                          /\\  /\\\n");
		sb.append("   _(    `.                                                       /  ''  \\\n");
		sb.append("  (        )     .  o ..                                         / .o\\  ..\\\n");
		sb.append(" ( `  .  )  )    o . o o.o                                      /.'  |\\ | '.\n");
		sb.append("  `--(___.-           ...oo_                                    '    |_\\|\n");
		sb.append("                          \\ \\___     .__                             |_|<o>\n");
		sb.append("                        .--\"\"___\\..--\"/                              |_| |\n");
		sb.append("                    .__.|-\"\"\"..... ' /    " + display
				+ "_____|_|_|\\__\n");
		sb.append("      ______________\\_______________/______________________..-'::::::::::::::::-..___\n");
		sb.append("                                                             '''\"\"\"\"\"\"\"\"\"\"\"\"\"\"'''\n");
		sb.append("                           ~       ~           ~        ~                  ~\n");
		sb.append("                   ~                     ~                   ~       ~\n");
		sb.append("              ~                 ~                  ~                         ~\n");
		sb.append("\n");
		logger.info(sb.toString());

	}

}
